#include<iostream>
#include<vector>
using namespace std;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */


struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
    bool judgePathSum(TreeNode* root, int targetSum, int tmpSum) {
        if (root == nullptr) return false;
        if (root->left == nullptr && root->right == nullptr) {
            if (tmpSum + root->val == targetSum)
                return true;
            return false;
        }
        return judgePathSum(root->left, targetSum, tmpSum + root->val) || judgePathSum(root->right, targetSum, tmpSum + root->val);
    }
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        return judgePathSum(root, targetSum, 0);
    }
};